package com.appkubes.base.core.mbp.impl;

import com.appkubes.base.core.mbp.AbstractBaseService;
import com.appkubes.base.core.entity.vo.PageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AbstractBaseServiceImpl<D extends BaseMapper<T>, T>
        extends ServiceImpl<D, T>
        implements AbstractBaseService<D, T> {

    @Autowired
    D dao;

    T entity;

    @Override
    public D getDao() {
        return dao;
    }

    @Override
    public T getOneByEntity(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.getOne(queryWrapper, false);
    }

    @Override
    public List<T> selectListByEntity(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<T> selectPageByEntity(IPage<T> page) {
        return selectPageByEntity(entity, page);
    }

    @Override
    public IPage<T> selectPageByEntity(PageVO pageVO) {
        return selectPageByEntity(entity, pageVO);
    }

    @Override
    public IPage<T> selectPageByEntity(T entity, IPage<T> page) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<T> selectPageByEntity(T entity, PageVO pageVO) {
        return selectPageByEntity(entity, pageVO.getPage(), pageVO.getSize());
    }

    @Override
    public IPage<T> selectPageByEntity(T entity, Integer pageNo, Integer pageSize) {
        Page page = new Page<>(pageNo, pageSize);
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Integer selectCount(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public Boolean removeByEntity(T entity) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(entity);
        return this.remove(queryWrapper);
    }
}
